package com.sh.caryr.erp.controller.product;


import com.sh.caryr.erp.entity.ResultMessage;
import com.sh.caryr.erp.entity.product.CombNum;
import com.sh.caryr.erp.exception.ResultCode;
import com.sh.caryr.erp.exception.RestServiceException;
import com.sh.caryr.erp.service.ICombNumService;
import com.sh.caryr.erp.util.UtilityTool;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套件规格表 前端控制器
 * </p>
 *
 * @author milkeeper
 * @since 2017-11-22
 */
@RestController
@RequestMapping("/api/combNum")
public class CombNumController {
    @Autowired
    private ICombNumService combNumService;

    @ApiOperation(value = "获取combNum列表信息", notes = "获取combNum列表信息")
    @RequestMapping(method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<List<CombNum>> findCombNums() {
        return new ResultMessage<>(combNumService.selectList(null));
    }

    @ApiOperation(value = "获取单个combNum信息", notes = "获取单个combNum信息")
    @ApiImplicitParam(name = "id", value = "combNumId", required = true, dataType = "long")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<CombNum> findCombNumById(@PathVariable long id){
        return  new ResultMessage<>(combNumService.selectById(id));
    }

    @ApiOperation(value="创建combNum", notes="根据combNum对象创建combNum")
    @ApiImplicitParam(name = "combNum", value = "combNum详细实体", required = true, dataType = "CombNum")
    @RequestMapping(method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<CombNum> insertCombNum(@RequestBody CombNum combNum) {
        if(combNumService.insert(combNum)) {
            return new ResultMessage<CombNum>(combNum);
        }else {
            throw new RestServiceException(ResultCode.CREATE_FAILED);
        }
    }

    @ApiOperation(value="更新combNum", notes="根据combNum对象更新combNum")
    @ApiImplicitParam(name = "combNum", value = "combNum详细实体", required = true, dataType = "CombNum")
    @RequestMapping(method=RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<CombNum> updateCombNumById(@RequestBody CombNum combNum) {
        if(combNumService.updateById(combNum)) {
            return new ResultMessage<CombNum>(combNum);
        }else {
            throw new RestServiceException(ResultCode.UPDATE_FAILED);
        }
    }

    @ApiOperation(value="批量删除combNum", notes="根据url的id来指定删除combNum")
    @ApiImplicitParam(name = "ids", value = "combNumIds", required = true, dataType = "String")
    @Transactional
    @RequestMapping(value = "/{ids}",method=RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage deleteCombNums(@PathVariable String ids) {
        List<Long> list = UtilityTool.convertStringArray2LongList(ids.split(","));
        if(combNumService.deleteBatchIds(list)){
            return new ResultMessage();
        }else {
            throw new RestServiceException(ResultCode.DELETE_NOT_FOUND);
        }
    }
}
